package com.gujiayue.common.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import io.netty.handler.codec.http.HttpMethod;

@Component
public class RequestFilter implements HandlerInterceptor {
	
	//拦截器设置跨域返回
	public boolean preHandler(HttpServletRequest request, HttpServletResponse response, Object handler) {
		response.setHeader("Access-Control-Allow-Origin", "http://localhost:4000");
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS");
		response.setHeader("Access-Control-Max-Age", "86400");
		response.setHeader("Access-Control-Allow-Headers", "sign");
		response.setContentType("application/json;charset=utf-8");
		// 如果是OPTIONS请求则结束
		if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
			response.setStatus(HttpStatus.NO_CONTENT.value());
			return false;
		}
		return true;
	}
}